﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BusinessLayer.Tables;

public partial class Admin_Donem : ChildPage
{
    #region Fields
    Db_Firma firma;
    Db_Donem donem;
    #endregion

    #region Function
    private void AttributeAdd()
    {
        imgbtnKaydetKapat.Attributes.Add("onclick", "return Kontrol();");
        imgbtnKaydet.Attributes.Add("onclick", "return Kontrol();");
        imgbtnSil.Attributes.Add("onclick", "return KontrolSil();");
    }
    private void CreateArea()
    {
        foreach (System.Web.UI.Control c in pnlControls.Controls)
        {
            if (c is CrmControls.CrmTextBox)
            {
                CrmControls.CrmTextBox text = c as CrmControls.CrmTextBox;
                text.Text = "";
            }
            if (c is CrmControls.CrmDropDown)
            {
                CrmControls.CrmDropDown drop = c as CrmControls.CrmDropDown;
                if (drop.SelectedIndex != -1)
                    drop.SelectedIndex = 0;
            }
        }
    }
    private void FirmaKod()
    {
        firma = new Db_Firma();
        DataTable dt = firma.GetAllRecord_Datatable("frm_id", "frm_adi");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            ListItem lst = new ListItem();
            lst.Value = dt.Rows[i][0].ToString();
            lst.Text = dt.Rows[i][1].ToString();
            drpFirma.Items.Add(lst);
        }
    }
    private void KayitBul(string id, string id2)
    {
        donem = new Db_Donem();
        if ((id != "" || id != null) && (id2 != "" || id2 != null))
        {
            donem.Dnm_Kod = id;
            donem.Dnm_Firma_ID = Convert.ToInt32(id2);
            donem.KayitBul();
            drpAktifYil.SelectedValue = donem.Dnm_Aktif_Yil.ToString();
            txtBitis_tarihi.Text = Convert.ToString(donem.Dnm_Bit_Date);
            drpFirma.SelectedValue = donem.Dnm_Firma_ID.ToString();
            txtDonemKod.Text = donem.Dnm_Kod;
            txtKullDate.Text = donem.Dnm_Kull_Date.ToString();
        }

    }
    private void Kaydet()
    {
        donem = new Db_Donem();
        if (txtDonemKod.Text != "" && txtBitis_tarihi.Text != "" && txtKullDate.Text != "")
        {
            donem.Create_Date = DateTime.Now;
            donem.Create_User = 1;
            if (drpAktifYil.SelectedIndex != -1)
                donem.Dnm_Aktif_Yil = int.Parse(drpAktifYil.SelectedValue);
            donem.Dnm_Bit_Date = DateTime.Parse(txtBitis_tarihi.Text);
            if (drpFirma.SelectedIndex != -1)
                donem.Dnm_Firma_ID = int.Parse(drpFirma.SelectedValue);
            donem.Dnm_Kod = txtDonemKod.Text;
            donem.Dnm_Kull_Date = DateTime.Parse(txtKullDate.Text);
            donem.Hidden = true;
            donem.Locked = true;
            donem.Update_Date = DateTime.Now;
            donem.Update_User = 1;
            donem.Save();
            CreateArea();
        }
        else
        {
            if (txtDonemKod.Text == "")
                CrmControls.MessageBx.Show("Dönem Kodu Boş Geçilmez!...");
            else if (drpAktifYil.SelectedIndex == -1)
                CrmControls.MessageBx.Show("Aktif Yıl Seçimi Boş Geçilmez!...");
            else if (txtKullDate.Text == "")
                CrmControls.MessageBx.Show("Başlangıç Tarihi Boş Geçilmez!...");
            else if (txtBitis_tarihi.Text == "")
                CrmControls.MessageBx.Show("Bitiş Tarihi Boş Geçilmez!...");
        }
    }
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        AttributeAdd();
        if (!IsPostBack)
        {
            if (Request.QueryString.Count > 0)
            {
                string id = Request.QueryString["id"].ToString();
                string id2 = Request.QueryString["id2"].ToString();
                KayitBul(id, id2);
            }
        }
    }
    protected override void OnInitComplete(EventArgs e)
    {
        FirmaKod();
        base.OnInitComplete(e);
    }
    protected void imgbtnKaydet_Click(object sender, ImageClickEventArgs e)
    {
        Kaydet();
        this.ClientScript.RegisterStartupScript(typeof(Page), "Yenile", "Refresh();", true);
    }
    protected void imgbtnKaydetKapat_Click(object sender, ImageClickEventArgs e)
    {
        Kaydet();
        this.Page.ClientScript.RegisterStartupScript(typeof(Page), "test", "PageClose();", true);
    }
    protected void imgbtnSil_Click(object sender, ImageClickEventArgs e)
    {
        donem = new Db_Donem();
        if (txtDonemKod.Text != "")
        {
            donem.Dnm_Kod = txtDonemKod.Text;
            if (drpFirma.SelectedIndex != -1)
                donem.Dnm_Firma_ID = int.Parse(drpFirma.SelectedItem.Value);
            donem.KayitSil();
            CreateArea();
        }
        else
            CrmControls.MessageBx.Show("Silmek İstediğiniz Dönem Kodunu veya Firmayı Seçin Girin!...");
        this.ClientScript.RegisterStartupScript(typeof(Page), "Tazele", "Refresh();", true);
    }
    protected void imgNewREcord_Click(object sender, ImageClickEventArgs e)
    {
        CreateArea();
    }
}
